<template>
  <FModal ref="refFModal" header="集团职务级别" @ok="ok" body-height="400">
    <el-table height="100%" border stripe :default-sort="sortConfig"
              @row-click="rowClick" :data="dataSource" @sort-change="sortChange"
              @selection-change="selectionChange" v-loading="loading">
      <el-table-column type="selection" width="40"/>
      <el-table-column show-overflow-tooltip prop="positionLevelName" label="职务级别名称"/>
      <el-table-column show-overflow-tooltip prop="positionLevelCode" label="职务级别编码"/>
      <el-table-column show-overflow-tooltip prop="positionLevel" label="职务级别"/>
      <el-table-column show-overflow-tooltip prop="sort" label="排序号"/>
    </el-table>
  </FModal>
</template>
<script setup>
import FModal from '@/components/FModal/index.vue';
import {shallowRef} from 'vue';
import TableSearchUtils from '@/utils/TableSearchUtils';
import {isEmpty, real} from '@/utils/CommonUtils';
import {getAction, postAction} from '@/request/BaseRequest';
import {ElMessage} from 'element-plus';

const refFModal = shallowRef();
let unitData = undefined;
const searchParams = {};
const emits = defineEmits(['copyedGroupPositionLevels']);

function setData(_unitData) {
  unitData = real(_unitData);
  refFModal.value.open();
  search();
}

const searchForm = {
  value: {
    validate(callback) {
      callback(true);
    },
  }
};

function ok() {
  let positionLevelIds = getPositionLevelIds();
  if (isEmpty(positionLevelIds)) {
    ElMessage({
      message: '请选择职务级别',
      type: 'warning',
    });
    return;
  }

  loading.value = true;
  postAction('/sys/positionLevel/copyGroupPositionLevels', {
    unitId: real(unitData).id,
    positionLevelIds
  }).then(data => {
    loading.value = false;
    if (data.success) {
      emits('copyedGroupPositionLevels');
      refFModal.value.close();
    }
  });

}

function getPositionLevelIds() {
  if (isEmpty(selectedRows)) return [];
  let list = real(selectedRows);
  list = list.map(item => item.id);
  return list;
}

function doSearch(params) {
  loading.value = true;
  getAction('/sys/positionLevel/loadGroupPositionLevel')
  .then(data => {
    loading.value = false;
    if (data.success) {
      let result = data.result;
      setSearchResult({
        records: result,
      });
    }
  });
}

function onSelection() {

}

function onRowClick() {

}

const {
  loading, rowClick, search, selectionChange, setSearchResult, pageOptions, pageChange,
  sortChange, dataSource, resetQueryParams, sortConfig, selectedRows, pagination
} = TableSearchUtils({
  refForm: searchForm,
  doSearch,
  defaultSortConfig: {
    prop: undefined,
    order: undefined,
  },
  searchParams,
  selectionChange: onSelection,
  rowClick: onRowClick
});

defineExpose({
  setData
});
</script>
<script>
export default {
  name: 'CopyGroupPositionLevelModal'
};
</script>
<style scoped>

</style>